//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package one.bugu.android.demon.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.juefeng.android.framework.common.util.Base64Util;

import one.bugu.android.demon.constant.Constant;

//import one.bugu.android.demon.ui.activity.MainActivity1;

public class PreferencesUtil {
    private String preferencesName = "LK_Util";
    private static PreferencesUtil instance;

    private PreferencesUtil() {
    }

    private PreferencesUtil(String preferenceName) {
        this.preferencesName = preferenceName;
    }

    public static PreferencesUtil getInstance(String preferenceName) {
        if (instance == null) {
            instance = new PreferencesUtil(preferenceName);
        } else {
            instance.setPreferencesName(preferenceName);
        }

        return instance;
    }

    public static PreferencesUtil getInstance() {
        if (instance == null) {
            instance = new PreferencesUtil();
        }

        return instance;
    }

    public boolean putString(Context context, String key, String value) {
        //保存的是token时对token进行base64加密
        if (TextUtils.equals(key, Constant.TOKEN)) {
            value = Base64Util.encode(value);
        }
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getString(Context context, String key) {
        String strValue = this.getString(context, key, (String) null);
        //获取的是token时对token进行base64加密
        if (TextUtils.equals(key, Constant.TOKEN)) {
            strValue = Base64Util.decode(strValue);
        }
        return strValue;
    }

    public String getString(Context context, String key, String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        return settings.getString(key, defaultValue);
    }

    public void remove(Context context, String key) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        settings.edit().remove(key).commit();
    }

    public void clear(Context context) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        settings.edit().clear().commit();
    }

    public boolean putInt(Context context, String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public int getInt(Context context, String key) {
        return this.getInt(context, key, -1);
    }

    public int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        return settings.getInt(key, defaultValue);
    }

    public boolean putLong(Context context, String key, long value) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public long getLong(Context context, String key) {
        return this.getLong(context, key, -1L);
    }

    public long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        return settings.getLong(key, defaultValue);
    }

    public boolean putFloat(Context context, String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    public float getFloat(Context context, String key) {
        return this.getFloat(context, key, -1.0F);
    }

    public float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        return settings.getFloat(key, defaultValue);
    }

    public boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public boolean getBoolean(Context context, String key) {
        return this.getBoolean(context, key, false);
    }

    public boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(this.preferencesName, 0);
        return settings.getBoolean(key, defaultValue);
    }

    public String getPreferencesName() {
        return this.preferencesName;
    }

    public void setPreferencesName(String preferencesName) {
        this.preferencesName = preferencesName;
    }
}
